/* eslint-env node */
const metadata = require('./metadata.json')
// console.log(metadata.RECORDS)

// MySQL 提供了多种数据类型，主要分为三大类：数值类型、日期和时间类型、字符串（字符）类型。
const datatypeMap = {
  number: [
    /^tinyint.*/,
    /^smallint.*/,
    /^mediumint.*/,
    /^int.*/,
    /^integer.*/,
    /^bigint.*/,
    /^float.*/,
    /^double.*/,
    /^decimal.*/
  ],
  string: [
    /^date.*/,
    /^time.*/,
    /^datetime.*/,
    /^timestamp.*/,
    /^year.*/,
    /^char.*/,
    /^varchar.*/,
    /^tinytext.*/,
    /^text.*/,
    /^mediumtext.*/,
    /^longtext.*/,
    /^binary.*/,
    /^varbinary.*/,
    /^enum.*/,
    /^set.*/,
    /^json.*/
  ]
}

/**
 * interface MetaDataUsers {
 *  id: number,
 *
 * }
 */

function getDataType(type) {
  let result = datatypeMap.number.find((v) => v.test(type))
  if (result) {
    return 'number'
  }
  result = datatypeMap.string.find((v) => v.test(type))
  return 'string'
}

const result = metadata.RECORDS.reduce((acc, row) => {
  const key = `interface MetaData${row.TABLE_NAME[0].toLocaleUpperCase()}${row.TABLE_NAME.slice(1)}`
  if (key in acc) {
    acc[key].push({
      name: row.COLUMN_NAME,
      type: getDataType(row.DATA_TYPE),
      required: row.IS_NULLABLE === 'NO'
    })
  } else {
    acc[key] = []
  }
  return acc
}, {})

console.log(result)
